provider "huaweicloud" {
  region     = "cn-north-4"
  access_key = ""
  secret_key = ""
}

data "huaweicloud_images_image" "default" {
  name        = "Ubuntu 18.04 server 64bit"
  most_recent = true
}


data "huaweicloud_compute_flavors" "web" {
  availability_zone = "cn-north-4a"
  performance_type  = "computingv3"
  cpu_core_count    = 2
  memory_size       = 4
}

resource "huaweicloud_vpc" "vpc" {
  count      = 1
  cidr       = "172.16.0.0/16"
  name       = "${var.template_name}-vpc"
}

resource "huaweicloud_vpc_subnet" "subnet" {
  vpc_id      = huaweicloud_vpc.vpc[0].id
  name        = "${var.template_name}-subnet"
  cidr        = "172.16.10.0/24"
  gateway_ip  = "172.16.10.1"
  primary_dns = "100.125.1.250"
}

resource "huaweicloud_networking_secgroup" "default" {
  name   ="${var.template_name}-secgroup"
  
}

resource "huaweicloud_networking_secgroup_rule" "allow_ping" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}



resource "huaweicloud_networking_secgroup_rule" "allow_http" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = 80
  port_range_max    = 80
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}


resource "huaweicloud_networking_secgroup_rule" "allow_https" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = 443
  port_range_max    = 443
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}

resource "huaweicloud_networking_secgroup_rule" "allow_elb_healthmonitor" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = 1
  port_range_max    = 65535
  remote_ip_prefix  = "100.125.0.0/16"
  security_group_id = huaweicloud_networking_secgroup.default.id
}


resource "huaweicloud_lb_loadbalancer" "internet" {
  depends_on = [huaweicloud_vpc_subnet.subnet]
  vip_subnet_id = huaweicloud_vpc_subnet.subnet.subnet_id
  name          = "${var.template_name}-elb"
}


resource "huaweicloud_vpc_eip" "myeip" {
  publicip {
    type = "5_bgp"
  }
  bandwidth {
    name        = "${var.template_name}-bandwidth"
    size        = 10
    share_type  = "PER"
    charge_mode = "traffic"
  }
}


resource "huaweicloud_networking_eip_associate" "eip_1" {
  depends_on = [huaweicloud_lb_loadbalancer.internet,huaweicloud_vpc_eip.myeip]
  public_ip = huaweicloud_vpc_eip.myeip.address
  port_id   = huaweicloud_lb_loadbalancer.internet.vip_port_id
}


resource "huaweicloud_lb_listener" "listener_1" {
  protocol        = "HTTP"
  protocol_port   = 80
  loadbalancer_id = huaweicloud_lb_loadbalancer.internet.id

  tags = {
    key = "value"
  }
}


resource "huaweicloud_lb_listener" "listener_2" {
  protocol        = "TCP"
  protocol_port   = 22
  loadbalancer_id = huaweicloud_lb_loadbalancer.internet.id

  tags = {
    key = "value"
  }
}


resource "huaweicloud_lb_pool" "group_1" {
  name        = "${var.template_name}-group1"
  protocol    = "HTTP"
  lb_method   = "ROUND_ROBIN"
  listener_id = huaweicloud_lb_listener.listener_1.id
}

resource "huaweicloud_lb_monitor" "monitor_http" {
  pool_id        = huaweicloud_lb_pool.group_1.id
  type           = "HTTP"
  delay          = 5
  timeout        = 3
  max_retries    = 3
  url_path       = "/"
}

resource "huaweicloud_lb_pool" "group_2" {
  name        = "${var.template_name}-group2"
  protocol    = "TCP"
  lb_method   = "ROUND_ROBIN"
  listener_id = huaweicloud_lb_listener.listener_2.id
}

resource "huaweicloud_lb_monitor" "monitor_tcp" {
  pool_id        = huaweicloud_lb_pool.group_2.id
  type           = "TCP"
  delay          = 5
  timeout        = 3
  max_retries    = 3
}

resource "huaweicloud_compute_keypair" "my_keypair" {
  name       = "${var.template_name}-keypair"
  key_file = "private_key.pem"
}


resource "huaweicloud_as_configuration" "my_as_config" {
  depends_on = [local_file.save_inventory,huaweicloud_compute_keypair.my_keypair]
  scaling_configuration_name = "${var.template_name}-asConfig"
  instance_config {
    flavor = data.huaweicloud_compute_flavors.web.ids[0]
    image  = data.huaweicloud_images_image.default.id
    key_name = "${var.template_name}-keypair"
    disk {
      size        = 40
      volume_type = "SSD"
      disk_type   = "SYS"
    }
  }
}

resource "huaweicloud_as_group" "my_as_group_with_enhanced_lb" {
  depends_on = [huaweicloud_as_configuration.my_as_config]
  scaling_group_name       = "${var.template_name}-asGroup"
  scaling_configuration_id = huaweicloud_as_configuration.my_as_config.id
  desire_instance_number   = 1
  min_instance_number      = 0
  max_instance_number      = 10
  vpc_id                   = huaweicloud_vpc.vpc[0].id
  delete_publicip          = true
  delete_instances         = "yes"
  networks {
    id = huaweicloud_vpc_subnet.subnet.id
  }
  security_groups {
    id = huaweicloud_networking_secgroup.default.id
  }
  lbaas_listeners {
    pool_id       = huaweicloud_lb_pool.group_1.id
    protocol_port = huaweicloud_lb_listener.listener_1.protocol_port
  }  
  lbaas_listeners {
    pool_id       = huaweicloud_lb_pool.group_2.id
    protocol_port = huaweicloud_lb_listener.listener_2.protocol_port
  } 
  tags = {
    owner = "AutoScaling"
  }
}


resource "huaweicloud_vpc_eip" "eip_2" {
  publicip {
    type = "5_bgp"
  }
  bandwidth {
    name        = "${var.template_name}-Bandwidth"
    size        = 5
    share_type  = "PER"
    charge_mode = "traffic"
  }
}


resource "huaweicloud_nat_gateway" "nat_1" {
  name          = "${var.template_name}-nat"
  spec          = "1"
  vpc_id        =  huaweicloud_vpc.vpc[0].id
  subnet_id     = huaweicloud_vpc_subnet.subnet.id
}


resource "huaweicloud_nat_snat_rule" "snat_1" {
  nat_gateway_id = huaweicloud_nat_gateway.nat_1.id
  subnet_id     = huaweicloud_vpc_subnet.subnet.id
  floating_ip_id = huaweicloud_vpc_eip.eip_2.id
}

data "huaweicloud_availability_zones" "myaz" {}


resource "huaweicloud_rds_instance" "default" {
  depends_on = [huaweicloud_vpc.vpc]
  count = 1
  name = "${var.template_name}-rds" 
  flavor = "rds.mysql.n1.large.2.ha"
  ha_replication_mode = "async"
  vpc_id = huaweicloud_vpc.vpc[0].id
  subnet_id           = huaweicloud_vpc_subnet.subnet.id
  security_group_id   = huaweicloud_networking_secgroup.default.id
  availability_zone   = [
    data.huaweicloud_availability_zones.myaz.names[0],
    data.huaweicloud_availability_zones.myaz.names[1]
  ] 
  db {
    type     = "MySQL"
    version  = "5.7"
    password = var.db_password
  }
  volume {
    type = "CLOUDSSD"
    size = 40
  }
  backup_strategy {
    start_time = "08:00-09:00"
    keep_days  = 1
  }
}

resource "local_file" "save_inventory" {
  content  = "${data.template_file.user_data.rendered}"
  filename = "./user1.sh"
}

resource "null_resource" "provision" {
  depends_on = [huaweicloud_rds_instance.default,huaweicloud_as_group.my_as_group_with_enhanced_lb,huaweicloud_compute_keypair.my_keypair]

  provisioner "local-exec" {
     command = format("hcloud configure set --cli-profile=testAKSK --cli-mode=AKSK --cli-region=%s --cli-access-key=%s --cli-secret-key=%s --cli-project-id=%s --cli-domain-id=%s --cli-read-timeout=10 --cli-connect-timeout=5 --cli-retry-count=3","cn-north-4","","",var.project_id,var.domain_id)
  }
   
  provisioner "local-exec" {
     command = "hcloud configure set --cli-lang=cn" 
  }
  
  provisioner "local-exec" {
     command = format("hcloud RDS CreateDatabase --cli-region=%s --instance_id=%s --project_id=%s --name=%s --character_set=%s","cn-north-4",huaweicloud_rds_instance.default[0].id,var.project_id,"magento_db2","utf8")
  }
  
  provisioner "file" {
    connection {
    type     = "ssh"
    user     = "root"
    private_key = file("./private_key.pem")
    host        = huaweicloud_vpc_eip.myeip.address
    }
    source = "./user1.sh"
    destination = "/tmp/user1.sh"
  }
  provisioner "remote-exec" {
    connection {
    type     = "ssh"
    user     = "root"
    private_key = file("./private_key.pem")
    host        = huaweicloud_vpc_eip.myeip.address
    }

    inline = [
      "sudo sh /tmp/user1.sh"
    ]
  }
}

data "template_file" "user_data" {
  template = file("./user_data.sh")

  vars = {
    DB_HOST_IP = "${huaweicloud_rds_instance.default[0].private_ips[0]}"
    DB_NAME = "magento_db2"
    DB_USER = "root"
    DB_PASSWORD = var.db_password
    DOMAIN_URL = "${huaweicloud_vpc_eip.myeip.address}"
    MAGENTO_ADMIN_USER = var.magento_admin_user
    MAGENTO_ADMIN_PASSWORD = var.magento_admin_passwd
    MAGENTO_ADMIN_EMAIL = var.magento_admin_email
    MAGENTO_ADMIN_FIRSTNAME = "Magento" 
    MAGENTO_ADMIN_LASTNAME = "Demo"
  }
}





